package student

import (
    "github.com/astaxie/beego/orm"
	_ "github.com/mattn/go-sqlite3"
	"beego/testApp/common/tool"
	"time"
)


type Student struct {
    Sid int `orm:"pk;auto;column(sid)"`
	Cid int 
	Sname string
	Age int
	Gender string
	Province string
	Created time.Time
}

func init(){
	
  
	tool.Pic("student pakcage init"," init")
	
	orm.RegisterModel(new(Student))
	
	/*
	orm.RegisterDriver("sqlite", orm.DRSqlite)
    orm.RegisterDataBase("default", "sqlite3", "./db/admin.db")
	orm.RunSyncdb("default", false, true)
	*/
	
	
}

func (s *Student) StuListByCid( cid  string ) []Student {
	
	 o := orm.NewOrm()
	 qs := o.QueryTable("student")

	 var stu []Student

	 num,err :=  qs.Filter("cid",cid).All(&stu)
	 
	 if(err!=nil){
	  tool.Pic("student StuListByCid ERROR" ,err )
	 }

	 tool.Pic("student StuListByCid success" , num )


	 return stu
	 //stu := &Student{}
	 //stu.Cid = cid
}



func (s *Student) Add() bool {

	o := orm.NewOrm()
	o.Using("default")
	id,err  := o.Insert(s)
	
	if(err!=nil){
	   
		tool.Pic("Student add() insert into Db FAIL",err)
        return false
	} 

	tool.Pic("Student add() insert into db succsess ",id)

   return true
}

